package com.apes.fn.scm.merchant.account.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.generator.Implementation;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.Arith;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;

/**
 * @ClassName: MerchantAccountBalance
 * @Author: muting
 * @CreateTime: 2023-02-23
 * @Description: 赋能商户结算对账单2.0 科目余额
 * @Version: 1.0
 */

@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = MerchantAccountSubjects.CONST_TABLE_NAME)
public class MerchantAccountSubjects extends BaseModel {
    public static final String CONST_TABLE_NAME = "merchant_account_subjects";

    @Id
    @Comment("账号编码")
    @Column(name = "id", nullable = false, length = 120)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "账号编码", padding = 4, implementation = Implementation.NO_GAP)
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "merchant_settlement_id", referencedColumnName = "id")
    @Comment("赋能商户结算对账单")
    private MerchantAccountSettlement merchantSettlement;

    @Comment("其他应付款-集团外-平台供货商")
    @Column(name = "platform_supplier", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double platformSupplier = 0;

    @Comment("其他应付款-集团外")
    @Column(name = "outside_group", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double outsideGroup = 0;

    @Comment("其他应付款-赋能")
    @Column(name = "empower", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double empower = 0;

    @Comment("其他应收款-集团外-赋能")
    @Column(name = "outside_group_empower", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double outsideGroupEmpower = 0;

    @Comment("开票录应付")
    @Column(name = "invoice_payable", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double invoicePayable = 0;

    @Comment("应录服务费")
    @Column(name = "accrual_service", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double accrualService = 0;

    @Comment("奖励")
    @Column(name = "reward", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double reward = 0;

    @Comment("总计应结算营业额")
    @Column(name = "amount", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double amount = 0;

    public double getAmount() {
        if(past==false) {
            amount= Arith.round(this.merchantSettlement.getSettleBusinessMoneyTotal(),2);
        }
        return amount;
    }

    @Comment("实际应付")
    @Column(name = "actual_payable", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double actualPayable = 0;

    public double getActualPayable() {
        double actualPayable = this.getPlatformSupplier() + this.getOutsideGroup() + this.getEmpower() - this.getOutsideGroupEmpower() + this.invoicePayable - this.accrualService - this.reward;
        return Arith.round(actualPayable,2);
    }


    @Comment("差异")
    @Column(name = "difference", columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double difference = 0;

    public double getDifference() {
        return Arith.round(this.getAmount() - this.getActualPayable(),2);
    }

    @Column(name = "note", length = 1000)
    @Comment("差异备注")
    private String note;

    @Column(name = "past", nullable = false,columnDefinition = "number(1,0) default 0")
    @Comment("往期标记")
    private Boolean past = false;


}
